package com.zt.springcloud.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author RedReins
 * @version 1.0
 * @description: 返回json信息
 * @date 2021/7/12 20:09
 */

@Data
public class ResultUtils<T> implements Serializable {
    private int code;
    private String msg;
    private T data;

    public static <T>ResultUtils<T> success(){
        return success(200,"请求成功",null);
    }
    public static <T> ResultUtils<T> success(T data){
        return success(200,"请求成功",data);
    }
    public static <T>ResultUtils<T> success(T data,String msg){
        return success(200,msg,data);
    }
    public static <T>ResultUtils<T> success(int code,String msg,T data){
        return result(code,msg,data);
    }

    public static <T>ResultUtils<T> fail(){
        return fail(500,"请求失败",null);
    }
    public static <T>ResultUtils<T> fail(T data){
        return fail(500,"请求失败",data);
    }
    public static <T>ResultUtils<T> fail(String msg){
        return fail(500,msg,null);
    }
    public static <T>ResultUtils<T> fail(T data,String msg){
        return fail(500,msg,data);
    }
    public static <T>ResultUtils<T> fail(int code,String msg,T data){
        return result(code,msg,data);
    }

    private static <T>ResultUtils<T> result(int code, String msg, T data){
        ResultUtils<T> apiResult = new ResultUtils<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }
}
